﻿Public Class EmkMaasHesap : Inherits Process

	Dim donem As Input
	Dim Rsm As Recordset

	Public Overrides Sub GenericEvent(ByVal e As ProcessEventArgs)
		Select Case e.EventType
			Case EventType.Initialize
				Start("Maaş hesabı", "Hesaplama dönemini seçin.")
				donem = AddDonem("donem", "Dönem", "nl", Today.YM, "Hesaplama dönemini seçmeniz gerekir.")
				StartBody()
				AddList()
				List.AddColumns("hturu(Hesap t.;60c),kararno(Karar no.;60c),kimlik(Kimlik no.;80c),adi(Adı;50%l),soyadi(Soyadı;50%l)")
				List.AddColumns("derece(D;30c),basamak(B;30c),yderece(YD;30c),ybasamak(YB;30c),katsayi1(K1;50c),katsayi2(K2;50c),ykatsayi1(YK1;50c)")
				List.AddColumns("maas(Maaş;80m),maas2(Maaş;80m)")
				List.ShowLineNumbers = True
			Case EventType.ValidateForm
				ShowProgress()
				Progress.ReportProgress("Kişi bilgileri okunuyor...")
				Rs = Emeklilik.Maas.AylikHakSahipleri(Remote)
				Rs.AddFields("maas(m),maas2(m)")
				Rsm = Emeklilik.Maas.Baremler(Remote, ToDate(donem.Get))
				List.DataSource = Rs
				Progress.ReportProgress("Maaşlar hesaplanıyor...", Rs.Count)
				AsyncRun(AddressOf Hesap)
				e.Result = True
			Case EventType.Progress
				Progress.Inc()
			Case EventType.AsyncCompleted
				Progress.Close()
				Help = "Maaşlar hesaplandı. Devam butonuyla kayıt işlemlerine geçebilirsiniz."
				List.DataSource = Rs
				Application.DoEvents()
				Kontrol()
				Buttons.Show("Next.Reset")
		End Select
	End Sub

	Private Sub Hesap()
		For Each Row As DataRow In Rs.Rows
			HesaplaKisi(Row)
			AsyncProgress(0, "")
		Next
	End Sub

	Private Sub HesaplaKisi(Row As DataRow)
		Dim ayarlama = IsNull(Row("ybasamak"), 0) > 0
		Dim derece As Integer = If(ayarlama, Row("yderece"), Row("derece"))
		Dim basamak As Integer = If(ayarlama, Row("ybasamak"), Row("basamak"))
		Dim katsayi As Integer = If(ayarlama, Row("ykatsayi1"), Row("katsayi1"))
		Dim esasmaas = Rsm("B" & Format(If(derece = 0, 95 + basamak, 1 + derece * 9 + basamak), "000"))
		Row("maas") = Math.Round(esasmaas * katsayi / 100000, 2)
	End Sub

	Private Sub Kontrol()
		Dim Rsm = Remote.OpenSQL("select maas from yaslılık..yasmaas where yil = @yil and ay = @ay order by hturu,kararno,kimlik", donem.yil, donem.ay)
		Dim i As Integer
		For Each Row In Rs
			If Rs("maas") <> Rsm("maas") Then
				Rs("maas2") = Rsm("maas")
				If Math.Abs(Rs("maas2") - Rs("maas")) > 0.01 Then Stop
			End If
			Rsm.MoveNext()
		Next
		List.DataSource = Rs
		If i = 0 Then
			Info("Başarılı")
		Else
			Warning(i & " kayıt hatalı.")
		End If
	End Sub

End Class
